package cn.maihe.elg.operation.security.model;

import cn.maihe.elg.operation.repository.entity.entityCustom.UserDOCustom;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.core.GrantedAuthority;

import java.util.List;

/**
 * @author vladimir.stankovic
 * <p>
 * Aug 4, 2016
 */
public class UserContext {
    private final String id;
    private final String username;
    private final String name;


    private final String orgCode;
    private final String orgName;
    private final Long orgId;
    private final Integer userType;
    private final Long bidderId;
    /**审核状态 0注册 1正常 2待内部审核 3待第三方审核 4审核不通过 5 删除*/
    private final Integer bidderStatus;
    private final List<GrantedAuthority> authorities;
    private final List<Long> centerIds;
    private final List<Long> institutionIds;
    private final UserDOCustom user;
    /**
     * 记账子单元编号
     */
    private String subAccount;


    private UserContext(String id, String username, List<GrantedAuthority> authorities, String orgCode,
                        Integer userType, String orgName, String name,Long orgId,
                        List<Long> centerIds,List<Long> institutionIds,UserDOCustom user,
                        Long bidderId,Integer bidderStatus,String subAccount) {
        this.username = username;
        this.id = id;
        this.authorities = authorities;
        this.orgCode = orgCode;
        this.orgName = orgName;
        this.userType = userType;
        this.orgId = orgId;
        this.name = name;
        this.centerIds = centerIds;
        this.institutionIds = institutionIds;
        this.user = user;
        this.bidderId = bidderId;
        this.bidderStatus = bidderStatus;
        this.subAccount = subAccount;
    }

    public UserContext(){
        this.username = null;
        this.id = null;
        this.authorities = null;
        this.orgCode = null;
        this.orgName = null;
        this.userType = null;
        this.orgId = null;
        this.name = null;
        this.centerIds = null;
        this.institutionIds = null;
        this.user = null;
        this.bidderId = null;
        this.bidderStatus = null;
    }

    public static UserContext create(String id, String username, List<GrantedAuthority> authorities, String orgCode,
                                     Integer userType, String orgName, String name,Long orgId,
                                     List<Long> centerIds,List<Long> institutionIds,UserDOCustom user,
                                     Long bidderId,Integer bidderStatus,String subAccount) {
        if (StringUtils.isBlank(username)) {
            throw new IllegalArgumentException("Username is blank: " + username);
        }
        return new UserContext(id, username, authorities, orgCode, userType, orgName, name,orgId,centerIds,institutionIds,user,bidderId,bidderStatus,subAccount);
    }

    public String getId() {
        return id;
    }

    public String getUsername() {
        return username;
    }

    public List<GrantedAuthority> getAuthorities() {
        return authorities;
    }

    public String getOrgCode() {
        return orgCode;
    }

    public String getOrgName() {
        return orgName;
    }
    public String getName() {
        return name;
    }

    public Integer getUserType() {
        return userType;
    }

    public boolean isSystemUser() {
        return "1".equals(this.id) || (this.userType != null && this.userType == 0);
    }

    public Long getOrgId() {
        return orgId;
    }

    public List<Long> getCenterIds() {
        return centerIds;
    }

    public List<Long> getInstitutionIds() {
        return institutionIds;
    }

    public UserDOCustom getUser() {
        return user;
    }

    public Long getBidderId() {
        return bidderId;
    }

    public Integer getBidderStatus() {
        return bidderStatus;
    }

    public String getSubAccount() {
        return subAccount;
    }
}
